package com.example.servlet;

import com.example.bean.Student;
import com.example.dao.StudentAddDaoImp;
import com.example.service.StudentAddService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "AddServlet", value = "/add")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.接收浏览器发来的信息
        request.setCharacterEncoding("utf-8");
        String sname = request.getParameter("sname");
        System.out.println("获取到的姓名为:"+sname);
        String ssex = request.getParameter("ssex");
        System.out.println("获取到的性别为:"+ssex);
        String sage = request.getParameter("sage");
        System.out.println("获取到的出生日期为:"+sage);
        String semail = request.getParameter("semail");
        System.out.println("获取到的邮箱为:"+semail);
        String stext = request.getParameter("stext");
        System.out.println("获取到的备注为:"+stext);

        //2.把获取到的信息打包起来给StudentAddDao
        Student student=new Student(sname,ssex,sage,semail,stext);
       //3.创建StudentAddService类型的对象去实现数据的添加功能
        //StudentAddService studentAddService=new StudentAddService();
        //studentAddService.StudentAddServer(new Student(sname,ssex,sage,semail,stext));
        StudentAddService studentAddService=new StudentAddService();
        int res = studentAddService.StudentAddServer(student);
        //4.添加成功 向浏览器发送成功
        response.setContentType("html/text;charset=utf-8");
        PrintWriter writer = response.getWriter();
        if(res==1) {
            writer.write("<h1>添加成功!</h1>");
            System.out.println("添加成功!");
        }else{
            writer.write("<h1>添加失败!</h1>");
        }
        writer.close();
    }
}
